﻿using Newtonsoft.Json;
using System.Collections.Generic;
using X.Core.Bases;

namespace X.Core.Models
{
    public class UshortDataModel : ModelBase
    {
        public UshortDataModel()
        {
            BitsX = new List<BitDataModel>();
            for (int i = 0; i < 16; i++)
            {
                var model = new BitDataModel() { Address = i };
                model.PropertyChanged += Model_PropertyChanged;
                BitsX.Add(model);
            }
            IsEnabled = true;
            CanUpdate = true;
        }
        public override (int, string) Fill<T>(T source, bool isCheckType = true, IEnumerable<string> ignorePropList = null)
        {
            var dat = source as UshortDataModel;
            base.Fill(dat);
            Group = dat.Group;
            GroupSub = dat.GroupSub;
            Address = dat.Address;
            PV = dat.PV;
            return (0, null);
        }
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (CanUpdate)
            {
                if (e.PropertyName == "BPV")
                {
                    Update();
                }
            }
        }

        ushort _pv;
        public List<BitDataModel> BitsX { get; set; }
        //public bool IsStatic { get; set; }
        public int Address { get; set; }
        public string AddressType { get; set; } = "D";
        //public bool CanDevide { get; set; } = false;
        public ushort PV
        {
            get => _pv; set
            {
                if (_pv != value)
                {
                    _pv = value;
                    int offset = 1;
                    for (int i = 0; i < 16; i++)
                    {
                        if ((value & offset) != 0)
                        {
                            BitsX[i].BPV = true;
                        }
                        else
                        {
                            BitsX[i].BPV = false;
                        }
                        offset <<= 1;
                    }
                    RaisePropertyChanged(nameof(PV));
                    IsChanged = true;
                }
            }
        }
        public void Update()
        {
            int index = 0;
            int pv = 0;

            foreach (var bit in BitsX)
            {
                if (bit.BPV)
                {
                    ushort dataTmp = (ushort)(1 << index);
                    pv |= dataTmp;
                }
                index++;
            }
            PV = (ushort)pv;
        }
        public bool CanUpdate { get; set; } = true;
        [JsonIgnore]
        public virtual bool Bit00 { get => (PV & 1) != 0; set { ushort dattmp = 1; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit00)); } }
        [JsonIgnore]
        public virtual bool Bit01 { get => (PV & 1 << 1) != 0; set { ushort dattmp = 1 << 1; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit01)); } }
        [JsonIgnore]
        public virtual bool Bit02 { get => (PV & 1 << 2) != 0; set { ushort dattmp = 1 << 2; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit02)); } }
        [JsonIgnore]
        public virtual bool Bit03 { get => (PV & 1 << 3) != 0; set { ushort dattmp = 1 << 3; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit03)); } }
        [JsonIgnore]
        public virtual bool Bit04 { get => (PV & 1 << 4) != 0; set { ushort dattmp = 1 << 4; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit04)); } }
        [JsonIgnore]
        public virtual bool Bit05 { get => (PV & 1 << 5) != 0; set { ushort dattmp = 1 << 5; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit05)); } }
        [JsonIgnore]
        public virtual bool Bit06 { get => (PV & 1 << 6) != 0; set { ushort dattmp = 1 << 6; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit06)); } }
        [JsonIgnore]
        public virtual bool Bit07 { get => (PV & 1 << 7) != 0; set { ushort dattmp = 1 << 7; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit07)); } }
        [JsonIgnore]
        public virtual bool Bit08 { get => (PV & 1 << 8) != 0; set { ushort dattmp = 1 << 8; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit08)); } }
        [JsonIgnore]
        public virtual bool Bit09 { get => (PV & 1 << 9) != 0; set { ushort dattmp = 1 << 9; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit09)); } }
        [JsonIgnore]
        public virtual bool Bit10 { get => (PV & 1 << 10) != 0; set { ushort dattmp = 1 << 10; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit10)); } }
        [JsonIgnore]
        public virtual bool Bit11 { get => (PV & 1 << 11) != 0; set { ushort dattmp = 1 << 11; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit11)); } }
        [JsonIgnore]
        public virtual bool Bit12 { get => (PV & 1 << 12) != 0; set { ushort dattmp = 1 << 12; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit12)); } }
        [JsonIgnore]
        public virtual bool Bit13 { get => (PV & 1 << 13) != 0; set { ushort dattmp = 1 << 13; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit13)); } }
        [JsonIgnore]
        public virtual bool Bit14 { get => (PV & 1 << 14) != 0; set { ushort dattmp = 1 << 14; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit14)); } }
        [JsonIgnore]
        public virtual bool Bit15 { get => (PV & 1 << 15) != 0; set { ushort dattmp = 1 << 15; if (value) { PV |= dattmp; } else { PV &= (ushort)~dattmp; } RaisePropertyChanged(nameof(Bit15)); } }
    }
}
